﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.Manufacturer;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using System.Linq.Expressions;
namespace WaterRevenueMS.WebUI.Controllers
{

    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ManufacturerController : AdminController
    {
        [Import]
        private IManufacturerService ManufacturerService { get; set; }

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var entity = new ManufacturerModel();   
            return View(entity);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = ManufacturerService.Manufacturers.Count(t => t.IsDeleted == false);
            var expr = BuildSearchCriteria();


            var filterResult = ManufacturerService.Manufacturers.Where(expr).Select(t => new ManufacturerModel
            {
                Id = t.Id,
                Shui_BCS=t.Shui_BCS,
                OrderNo = t.OrderNo,
                Chang_SMC=t.Chang_SMC,
                Chang_SDZ=t.Chang_SDZ,
                Chang_SJJ=t.Chang_SJJ,
                Dian_H=t.Dian_H,
                Chuan_Z=t.Chuan_Z,
                Lian_XR=t.Lian_XR

            }).OrderBy(t => t.OrderNo).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Chang_SMC,
                            c.Dian_H,
                            c.Chuan_Z,
                            c.Lian_XR,
                            c.Chang_SDZ,
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new ManufacturerModel();

            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(ManufacturerModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = ManufacturerService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new ManufacturerModel();
            var entity = ManufacturerService.Manufacturers.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new ManufacturerModel
                {
                    Shui_BCS = entity.Shui_BCS,
                    OrderNo = entity.OrderNo,
                    Chang_SMC = entity.Chang_SMC,
                    Chang_SJJ = entity.Chang_SJJ,
                    Chang_SDZ = entity.Chang_SDZ,
                    Dian_H = entity.Dian_H,
                    Chuan_Z = entity.Chuan_Z,
                    Lian_XR = entity.Lian_XR
                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(ManufacturerModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = ManufacturerService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = ManufacturerService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<Manufacturer, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<Manufacturer> bulider = new DynamicLambda<Manufacturer>();
            Expression<Func<Manufacturer, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Chang_SMC"]))
            {
                var data = Request["Chang_SMC"].Trim();
                Expression<Func<Manufacturer, Boolean>> tmp = t => t.Chang_SMC.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Chang_SJJ"]))
            {
                var data = Request["Chang_SJJ"].Trim();
                Expression<Func<Manufacturer, Boolean>> tmp = t => t.Chang_SJJ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Chang_SDZ"]))
            {
                var data = Request["Chang_SDZ"].Trim();
                Expression<Func<Manufacturer, Boolean>> tmp = t => t.Chang_SDZ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Dian_H"]))
            {
                var data = Request["Dian_H"].Trim();
                Expression<Func<Manufacturer, Boolean>> tmp = t => t.Dian_H.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            Expression<Func<Manufacturer, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }


    }
}
    